package com.yoomiito.app.adapter.my;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.model.my.MyCollectGoodsBean;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.CouponView;

import java.util.List;

/**
 * 清单列表adapter
 */

public class MyCollectGoodsAdapter extends BaseRecyclerViewAdapter<MyCollectGoodsBean.DataBean> {

    private OnDeleteClickLister mDeleteClickListener;

    public MyCollectGoodsAdapter(Context context, List<MyCollectGoodsBean.DataBean> data) {
        super(context, data, R.layout.item_my_collect_goods);
    }

    public void setOnDeleteClickListener(OnDeleteClickLister listener) {
        this.mDeleteClickListener = listener;
    }

    @Override
    protected void onBindData(com.yoomiito.app.adapter.my.RecyclerViewHolder helper, MyCollectGoodsBean.DataBean item, int position) {
        View view = helper.getView(R.id.ll_hidden);
        view.setTag(position);
        if (!view.hasOnClickListeners()) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDeleteClickListener != null) {
                        mDeleteClickListener.onDeleteClick(v, (Integer) v.getTag());
                    }
                }
            });
        }

        ImageView ivPic = (ImageView) helper.getView(R.id.iv_list);
        TextView titleTv = (TextView) helper.getView(R.id.tv_list_title);
        TextView priceTv = (TextView) helper.getView(R.id.tv_voucher_Rear);
        helper.getView(R.id.tv_share_ll).setVisibility(View.GONE);
        helper.getView(R.id.tv_share_ll_store).setVisibility(View.VISIBLE);
        TextView storeNameTv = (TextView) helper.getView(R.id.tv_share_store_name);
        TextView saleNumTv = (TextView) helper.getView(R.id.tv_share_saleNum);
        CouponView awardView = (CouponView) helper.getView(R.id.awardView);
        CouponView couponView = (CouponView) helper.getView(R.id.couponView);
//        int resId = 0;
//        switch (item.getSource()) {
//            case 0:
//                resId = R.mipmap.tb;
//                break;
//            case 1:
//                resId = R.mipmap.tmall;
//                break;
//        }
        ImageLoader.getInstance().into(ivPic.getContext(), item.getImageUrl(), ivPic);

//        setTitle(titleTv,item.getTitle(), resId);
        titleTv.setText(SpannableUtils.getIconAndText(item.getTitle(), ResUtil.getDrawable(item.getSource()==1?R.mipmap.tmall:R.mipmap.tb)));
//        setGiftAndCoupon(iv_1,ll_1,bonusTv,item.getBuyAwardPrice());
//        setGiftAndCoupon(iv_2,ll_2,voucher,item.getCoupon_price());
        awardView.setVisibility(SpannableUtils.isEmptyOr0(item.getBuyAwardPrice())?View.GONE:View.VISIBLE);
        awardView.setText("¥"+item.getBuyAwardPrice());

        couponView.setVisibility(SpannableUtils.isEmptyOr0(item.getCoupon_price())?View.GONE:View.VISIBLE);
        couponView.setText("¥"+item.getCoupon_price());

        priceTv.setText(SpannableUtils.handlePrice(item.getDiscountPrice()));
        saleNumTv.setText("月销"+SpannableUtils.saleNum(item.getSalesNum()));
        storeNameTv.setText(item.getNick());
    }

    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);
    }

    private void setGiftAndCoupon(ImageView iv, View view, TextView tv, String value) {
//        boolean isCoupon = TextUtils.isEmpty(value)||"0".equals(value)||"0.0".equals(value)||"0.00".equals(value);
        boolean isCoupon = SpannableUtils.isEmptyOr0(value);
        iv.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        view.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        if (!isCoupon) tv.setText("¥" + SpannableUtils.handlePrice(value));
    }
}